package contacts.view;

import java.awt.Color;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.eltima.components.ui.DatePicker;
import contacts.controller.ContactsControllerListener;
import contacts.po.Room;
import contacts.po.Student;

public class ContactsDialog extends JDialog{
	private static final long serialVersionUID = 1L;
	private ContactsControllerListener contactsControllerListener;
	private JTextField idTextField,sNumberTextField,sNameTextField,birthdayTextFieldExample,birthdayTextField,telTextField,emailTextField,houseAddressTextField,sRoomNoTextField,sRoomNameTextField;//
	private JTextField sRoomNoTextField2,sRoomNameTextField2;
	private JButton confirmButton;
	private JPanel contactsAddPanel;
	private Student student;
	private Room room;
	public ContactsDialog(ContactsFrame owner,String title,Student student) {
		this.contactsControllerListener=owner.getControllerListener();
		this.contactsControllerListener.setContactsAddDialog(this);
		this.setTitle(title);
		this.student=student;
		this.add(getContactsAddPanel());//
		this.setModal(true);
		this.setSize(300,600);//这里改添加框大小
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.validate();
	}
	
	public ContactsDialog(ContactsFrame owner,String title) {
		this.contactsControllerListener=owner.getControllerListener();
		this.contactsControllerListener.setContactsAddDialog(this);
		this.setTitle(title);
		this.add(getContactsAddPanel());//
		this.setModal(true);
		this.setSize(300,600);//这里改添加框大小
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.validate();
	}
	
	public  ContactsDialog(ContactsFrame owner,String title,Room room) {
		this.contactsControllerListener=owner.getControllerListener();
		this.contactsControllerListener.setContactsAddDialog(this);
		this.setTitle(title);
		this.room=room;
		this.add(getContactsAddPanel2());//
		this.setModal(true);
		this.setSize(300,300);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.validate();
	}
	public  ContactsDialog(String title,ContactsFrame owner) {
		this.contactsControllerListener=owner.getControllerListener();
		this.contactsControllerListener.setContactsAddDialog(this);
		this.setTitle(title);
		this.add(getContactsAddPanel2());//
		this.setModal(true);
		this.setSize(300,300);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.validate();
	}
	
	public void closeCurrentDialog() {
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}
	
	private JPanel getContactsAddPanel() {
		contactsAddPanel = new JPanel();
		contactsAddPanel.setBackground(Color.PINK);
		contactsAddPanel.setBounds(200, 200, 200, 200);
		if("添加学生".equals(this.getTitle())) {
			idTextField = new JTextField(20);
			sNumberTextField = new JTextField(20);
			sNameTextField = new JTextField(20);
			
			birthdayTextFieldExample = new JTextField(16);//这个长度不要随便改		
			DatePicker datepick;		
	        datepick = getDatePicker();
	        birthdayTextFieldExample.add(datepick);
	        birthdayTextFieldExample.setVisible(true);	
	        System.out.println(datepick.getValue());
			
	        birthdayTextField = new JTextField(20);
	        telTextField = new JTextField(18);
			emailTextField = new JTextField(20);
			houseAddressTextField = new JTextField(20);
			sRoomNoTextField = new JTextField(18);					
			confirmButton=new JButton("确定添加");
			confirmButton.setActionCommand("addPost");
		}
		else  {
			idTextField = new JTextField(String.valueOf(student.getId()),20);
			sNumberTextField = new JTextField(student.getsNumber(),20);
			sNameTextField = new JTextField(student.getsName(),20);
			
			birthdayTextFieldExample = new JTextField(String.valueOf(student.getBirthday()),16);
			final DatePicker datepick;		
	        datepick = getDatePicker();
	        birthdayTextFieldExample.add(datepick);
	        birthdayTextFieldExample.setVisible(true);	
	        
	        birthdayTextField= new JTextField(String.valueOf(student.getBirthday()),20);
	        	     
			telTextField = new JTextField(student.getTel(),18);
			emailTextField = new JTextField(student.getEmail(),20);
			houseAddressTextField = new JTextField(student.getHouseAddress(),20);
			sRoomNoTextField = new JTextField(String.valueOf(student.getRoom().getRoomNo()),18);			
				   
			confirmButton=new JButton("确定修改");
			confirmButton.setActionCommand("updatePost");
		}	
		confirmButton.addActionListener(contactsControllerListener);
		contactsAddPanel.add(new JLabel("学生编号"));
		contactsAddPanel.add(idTextField);
		contactsAddPanel.add(new JLabel("学生学号"));
		contactsAddPanel.add(sNumberTextField);
		contactsAddPanel.add(new JLabel("学生姓名"));
		contactsAddPanel.add(sNameTextField);
		
		contactsAddPanel.add(new JLabel("学生生日选择"));
		contactsAddPanel.add(birthdayTextFieldExample);
		
		contactsAddPanel.add(new JLabel("学生生日"));
		contactsAddPanel.add(birthdayTextField);
		contactsAddPanel.add(new JLabel("学生电话号码"));
		contactsAddPanel.add(telTextField);
		contactsAddPanel.add(new JLabel("学生邮件"));
		contactsAddPanel.add(emailTextField);
		contactsAddPanel.add(new JLabel("学生地址"));
		contactsAddPanel.add(houseAddressTextField);
		contactsAddPanel.add(new JLabel("学生宿舍号"));
		contactsAddPanel.add(sRoomNoTextField);		
		contactsAddPanel.add(confirmButton);
		return contactsAddPanel;
	}
	
	private JPanel getContactsAddPanel2() {
		contactsAddPanel = new JPanel();
		contactsAddPanel.setBackground(Color.PINK);
		contactsAddPanel.setBounds(200, 200, 100, 100);
	if("添加宿舍".equals(this.getTitle())) {
		sRoomNoTextField2 = new JTextField(20);			
		sRoomNameTextField2 = new JTextField(20);			
		confirmButton=new JButton("确定添加");
		confirmButton.setActionCommand("addPost2");				
	}	
	else {
		sRoomNoTextField2 = new JTextField(String.valueOf(room.getRoomNo()),20);			
		sRoomNameTextField2 = new JTextField(room.getRoomName(),20);
		confirmButton=new JButton("确定修改");
		confirmButton.setActionCommand("updatePost2");
	}
	
	confirmButton.addActionListener(contactsControllerListener);
	contactsAddPanel.add(new JLabel("宿舍号"));
	contactsAddPanel.add(sRoomNoTextField2);		
	contactsAddPanel.add(new JLabel("宿舍名"));
	contactsAddPanel.add(sRoomNameTextField2);	
	contactsAddPanel.add(confirmButton);
	return contactsAddPanel;
	}
	
	public JTextField getIdTextField() {
		return idTextField;
	}
	
	public JTextField getsNumberTextField() {
		return sNumberTextField;
	}
	
	public JTextField getsNameTextField() {
		return sNameTextField;
	}
	
	public JTextField getBirthdayTextField() {
		return birthdayTextField;
	}
	
	public JTextField getTelTextField() {
		return telTextField;
	}
	
	public JTextField getEmailTextField() {
		return emailTextField;
	}
	
	public JTextField getHouseAddressTextField() {
		return houseAddressTextField;
	}
	
	public JTextField getsRoomNoTextField() {
		return sRoomNoTextField;
	}
	
	public JTextField getsRoomNameTextField() {
		return sRoomNameTextField;
	}
	
	public JButton getConfirmButton() {
		return confirmButton;
	}
	
	public JTextField getsRoomNoTextField2() {
		return sRoomNoTextField2;
	}
	
	public JTextField getsRoomNameTextField2() {
		return sRoomNameTextField2;
	}
	
	public DatePicker getDatePicker() {		
		DatePicker datepick;
        String DefaultFormat = "yyyy-MM-dd";
        Date date = new Date();
        Font font = new Font("Times New Roman", Font.BOLD, 14);
        Dimension dimension = new Dimension(177, 24);
        int[] hilightDays = { 1, 3, 5, 7 };
        int[] disabledDays = { 4, 6, 5, 9 };
        datepick = new DatePicker(date, DefaultFormat, font, dimension);
        datepick.setHightlightdays(hilightDays, Color.red);
        datepick.setDisableddays(disabledDays);
        return datepick;	
	}
	 
	
}

